<?php

namespace App\Http\Controllers;

use App\Club;
use App\Http\Requests\AddClubRequest;
use App\MakeTable\Table;
use App\Redict\Success;
use Illuminate\Http\Request;
use Gate;
use Auth;

class ClubController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //


        if (Gate::denies('club_Admin_club')) {
            return redirect('admin/clubs/'.Auth::user()->clubroleid().'/edit');
        }//权限判断
        $pagename = '俱乐部管理';
        $modelurl = 'clubs';
        $tablecate = ['tablecate'=>'俱乐部'];
        $headname = ['俱乐部名','地址','联系电话','营业时间','关注者数','点赞数','简介','更新时间','轮播图','操作'];
        $tablehead = ['name','address','contact_tele','opentime','follow_count','likes_count','intro','updated_at','clubbanners','id'];
        $clubtable = new Table($tablehead,$page = 8);
        $model = $clubtable->viewdata(Club::class);
        $models = $clubtable->viewpagedata(Club::class);
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $modelurl = 'clubs';
        $pagename = '添加俱乐部';
        $formhead = ['name','address','contact_tele','opentime','intro'];
        $formname = ['俱乐部名','地址','联系方式','开放时间','简介'];
        $formhead = collect($formname)->zip($formhead)->toArray();

        return view('admin.home.clubform',compact('pagename','tags','modelurl','formhead'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\AddClubRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddClubRequest $request)
    {
        //
        $data = $request->all();

        $club = Club::create($data);


        return redirect('admin/clubs')->with('message', '成功添加'.$club->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $club = Club::findOrFail($id);
        if (Gate::denies('doitself',$club)) {
            return redirect('admin/');
        }

        $modelurl = 'clubs';

        $pagename = $club->name;



        $formhead = ['name','address','contact_tele','opentime','intro'];
        $formname = ['俱乐部名','地址','联系方式','开放时间','简介'];
        $formhead = collect($formname)->zip($formhead)->toArray();

        return view('admin.home.editclub',compact('pagename','club','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AddClubRequest $request, $id)
    {
        //
        $club = Club::findOrFail($id);

        $data = $request->all();

        $club->update($data);
        //
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Club::destroy($id);
        return Success::back();
    }
}
